/**
 * Problem.java
 * Created on 2006-10-30
 * 
 */
package org.idontknow.packing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

/**
 * @author Zhao Xinyu
 * 
 * this is toooooooooooooooo simple an algo to concieve!
 */
public class Packing {

	private static Logger log = Logger
			.getLogger(org.idontknow.packing.Packing.class.getName());

	public static void main(String[] args) {

		// BasicConfigurator.configure();
		PropertyConfigurator.configure("log4j.properties");
		String[] orderOfBins = { "BCG", "BGC", "CBG", "CGB", "GBC", "GCB" };
		int[] nbMoves = new int[6];
		int[] nbBottles = new int[9];
		int temp;
		String line;

		try {
			FileReader fdr = new FileReader(System.getProperty("user.dir")
					+ File.separator + "src" + File.separator + "org"
					+ File.separator + "idontknow" + File.separator + "packing"
					+ File.separator + "inputPacking");
			BufferedReader bdr = new BufferedReader(fdr);

			FileOutputStream fos = new FileOutputStream(System
					.getProperty("user.dir")
					+ File.separator
					+ "output"
					+ File.separator
					+ "packing_output");
			PrintStream ps = new PrintStream(fos);

			while ((line = bdr.readLine()) != null) {

				log.debug("Got input line: " + line);

				StringTokenizer st = new StringTokenizer(line);
				for (int i = 0; i < 9; i++) {
					if (!st.hasMoreTokens()) {
						log.fatal("Got an incomplete line! Bad input file!");
						System.exit(-1);
					}
					nbBottles[i] = Integer.parseInt(st.nextToken());
				}

				log.debug("In the first bin, we got: " + nbBottles[0]
						+ " brown bottles, " + nbBottles[1]
						+ " green bottles and " + nbBottles[2]
						+ " clear bottles.");
				log.debug("In the second bin, we got: " + nbBottles[3]
						+ " brown bottles, " + nbBottles[4]
						+ " green bottles and " + nbBottles[5]
						+ " clear bottles.");
				log.debug("In the third bin, we got: " + nbBottles[6]
						+ " brown bottles, " + nbBottles[7]
						+ " green bottles and " + nbBottles[8]
						+ " clear bottles.");

				// there are some common bottles to count for "BCG" and "BGC"

				temp = nbBottles[1] + nbBottles[2] + nbBottles[3]
						+ nbBottles[6];
				// calculate for order "BGC" of three bins
				log.debug("Now counting movings for \"BGC\" ordered bins...");
				nbMoves[1] = temp + nbBottles[5] + nbBottles[7];
				log.debug("ok, \"BGC\" order needs " + nbMoves[1] + " movings");
				// calculate for order "BCG" of three bins
				log.debug("Now counting movings for \"BCG\" ordered bins...");
				nbMoves[0] = temp + nbBottles[4] + nbBottles[8];
				log.debug("ok, \"BCG\" order needs " + nbMoves[0] + " movings");

				// there are some common bottles to count for "CBG" and "CGB"
				temp = nbBottles[0] + nbBottles[1] + nbBottles[5]
						+ nbBottles[8];
				// calculate for order "CBG" of three bins
				log.debug("Now counting movings for \"CBG\" ordered bins...");
				nbMoves[2] = temp + nbBottles[4] + nbBottles[6];
				log.debug("ok, \"CBG\" order needs " + nbMoves[2] + " movings");
				// calculate for order "CGB" of three bins
				log.debug("Now counting movings for \"CGB\" ordered bins...");
				nbMoves[3] = temp + nbBottles[3] + nbBottles[7];
				log.debug("ok, \"CGB\" order needs " + nbMoves[3] + " movings");

				// there are some common bottles to count for "GBC" and "GCB"
				temp = nbBottles[0] + nbBottles[2] + nbBottles[4]
						+ nbBottles[7];
				// calculate for order "GBC" of three bins
				log.debug("Now counting movings for \"GBC\" ordered bins...");
				nbMoves[4] = temp + nbBottles[5] + nbBottles[6];
				log.debug("ok, \"GBC\" order needs " + nbMoves[4] + " movings");
				// calculate for order "GCB" of three bins
				log.debug("Now counting movings for \"GCB\" ordered bins...");
				nbMoves[5] = temp + nbBottles[3] + nbBottles[8];
				log.debug("ok, \"GCB\" order needs " + nbMoves[5] + " movings");

				temp = getMin(nbMoves);
				log
						.debug("Solved one line: the order leading to least movings is "
								+ orderOfBins[temp]);
				log.debug("In this order, we need " + nbMoves[temp]
						+ " moves to finish the packing");
				System.out.println(orderOfBins[temp] + " " + nbMoves[temp]);
				ps.println(orderOfBins[temp] + " " + nbMoves[temp]);
			}
			bdr.close();
			fdr.close();
			ps.close();
			fos.close();
		} catch (FileNotFoundException e) {
			log.fatal("Can't open input file!");
			e.printStackTrace();
		} catch (IOException e) {
			log.fatal("Error in reading input file!");
			e.printStackTrace();
		}
	}

	private static int getMin(int[] array) {
		log.debug("In method: getMix(int[] array)");
		log.debug("Getting minimum number of movings...");
		if (array == null)
			log.fatal("Fatal error: got an empty array in method getMin!");
		int minElement = array[0];
		int minIdx = 0;
		for (int i = 1; i < array.length; i++) {
			if (array[i] < minElement) {
				minElement = array[i];
				minIdx = i;
			}
		}
		log.debug("Finished min calculation, got idx " + minIdx
				+ ", it contains a min value of " + minElement);
		log.debug("Getting out of method getMix(int[] array)...");
		return minIdx;
	}
}
